﻿using System;
using System.Diagnostics.Contracts;
using System.Linq;
using IMModel.Domain.Common;

namespace IMModel.Domain
{
    /// <summary>
    /// Определяет категорию в разделе в определённом периоде
    /// </summary>
    public partial class CategorySectionTerm : IRespondentIndexVector, ICacheble, IItem
    {
        [ContractInvariantMethod]
        void CategorySectionInvariant()
        {
            Contract.Invariant(_respondentIndexCollector != null);                    
        }

        private IIndexCollectors<CategorySectionTerm, CharacteristicTerm> _respondentIndexCollector = 
            CategorySectionTermRespondentIndexCollectionProvider.Instance;

        private double? _totalWeight;

        public IIndexCollectors<CategorySectionTerm, CharacteristicTerm> RespondentIndexCollectionProvider
        {
            get
            {
                return _respondentIndexCollector;
            }
            set
            {
                Contract.Assert(_respondentIndexCollector != null);
                Contract.Assert(_respondentIndexCollector == value);
                _respondentIndexCollector = value;
            }
        }

        /// <summary>
        /// Получает суммарный вес всех показателей в категории
        /// </summary>
        public double TotalWeight
        {
            get
            {
                Contract.Assert(CharacteristicTerms != null);
                if (_totalWeight == null) _totalWeight = CharacteristicTerms.Sum(x => x.Weight);
                return _totalWeight.Value;
            }
        }

        public IItem Parent
        {
            get { return SectionTerm; }
        }

        public double Weight
        {
            get { return CategoryTerm.Weight; }
        }

        public System.Collections.Generic.IEnumerable<IItem> Items
        {
            get { return CharacteristicTerms; }
        }
    }
}